package org.dydl.service.system.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IGoodsRepository;
import org.dydl.model.system.Goods;
import org.dydl.model.system.Product;
import org.dydl.service.system.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService {

	@Autowired
	private IGoodsRepository goodsRepository;

	@Override
	public ResponseMessage save(Product product) {
		ResponseMessage res = new ResponseMessage();
		try {
			product.setCreateUser("admin");
			product.setCreateDate(new Timestamp(new Date().getTime()));
			product.setTypeId(123123);
			Product save = goodsRepository.save(product);
			res.setSuccess(true);
			res.setData(save);
		} catch (Exception e) {
			e.printStackTrace();
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;

	}

	@Override
	public ResponseMessage delete(int id) {
		goodsRepository.delete(id);
		ResponseMessage res = new ResponseMessage(true);
		return res;
	}

	@Override
	public ResponseMessage getGoodsList(int page, int rows, String name) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Goods> pageList = null;
		if (StringUtils.isNotBlank(name)) {
			pageList = goodsRepository.getGoodsListLike(
					String.format("%s%s%s", "%", StringUtils.trim(name), "%"),
					pageable);
		} else {
			pageList = goodsRepository.getGoodsList(pageable);
		}

		ResponseMessage responseMessage = new ResponseMessage(true,
				new ResponseData<Goods>(page, pageList.getTotalPages(),
						((int) pageList.getTotalElements()),
						pageList.getContent()));

		return responseMessage;
	}

	@Override
	public List<Goods> get(String sku) {
		List<Goods> res = goodsRepository.get(sku);

		return res;
	}

	@Override
	public void saveMaterial(List<Product> goods) {
		for (Product goods2 : goods) {
			goodsRepository.save(goods2);
		}
	}

	@Override
	public List<Product> findByName(String name) {
		List<Product> res = goodsRepository.findByName(name);

		return res;
	}

	@Override
	public ResponseMessage getGoodLists(String name, Pageable pageable) {
		ResponseMessage responseMessage = new ResponseMessage();
		try {
			Page<Product> page = goodsRepository.findAll(
					new Specification<Product>() {
						@Override
						public javax.persistence.criteria.Predicate toPredicate(
								Root<Product> root,
								CriteriaQuery<?> criteriaQuery,
								CriteriaBuilder criteriaBuilder) {
							List<Predicate> predicatesList = new ArrayList<Predicate>();
							if (StringUtils.isNotBlank(name)) {
								predicatesList.add(criteriaBuilder
										.and(criteriaBuilder.like(
												root.get("name").as(
														String.class), "%"
														+ name + "%")));
							}
							criteriaQuery.where(predicatesList
									.toArray(new Predicate[predicatesList
											.size()]));
							return null;
						}
					}, pageable);
			responseMessage.setSuccess(true);
			responseMessage.setData(new ResponseData<Product>(pageable
					.getPageSize(), page.getTotalPages(), ((int) page
					.getTotalElements()), page.getContent()));
		} catch (Exception e) {
			e.printStackTrace();
			responseMessage.setSuccess(false);
			responseMessage.setMsg(e.getMessage());
		}
		return responseMessage;
	}

	@Override
	public ResponseMessage getGoodsBySku(String sku, String type) {
		ResponseMessage res = new ResponseMessage();
		try {
			List<Goods> goods = goodsRepository.getGoodsBySku(sku, type);
			res.setSuccess(true);
			res.setData(goods);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}

	@Override
	public ResponseMessage getMaterialByName(String name) {
		ResponseMessage res = new ResponseMessage();
		try {
			List<Goods> goods = goodsRepository.getMaterialByName(name);
			res.setSuccess(true);
			res.setData(goods);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}

	@Override
	public List<Product> getGoodsByName(String name) {
		List<Product> goods = goodsRepository.getGoodsByName(name);
		return goods;

	}
}
